﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.DTO;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class AdminUsersController : Controller
    {
        //反转注入
        private IServiceAdminUsers _AdminUsers;
        private IServiceRoles _role;
        public AdminUsersController(IServiceAdminUsers AdminUsers, IServiceRoles roles)
        {
            _AdminUsers = AdminUsers;
            _role = roles;
        }
        /// <summary>
        /// 管理员列表(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 管理员列表显示(方法)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult AdminList(int start, int length, string name)
        {
            int count = 0;
            var list = _AdminUsers.GetAdminlist(start, length, name, ref count);
            PageModel page = new PageModel();
            page.data = list;
            page.recordsTotal = count;
            page.recordsFiltered = count;
            return Json(page);
        }
        /// <summary>
        /// 添加管理员(视图)
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            var roleList = _role.GetList();
            ViewBag.roleList = roleList;
            var cityList = _AdminUsers.GetCity();
            ViewBag.CityList = cityList;
            return View();
        }
        /// <summary>
        /// 实现添加管理员(方法)
        /// </summary>
        /// <param name="adminUsers"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddAdmin(AdminDTO adminUsers)
        {
            var list = _AdminUsers.Add(adminUsers);
            return Json(list);
        }
        /// <summary>
        /// 根据id查询出该条数据并传递给视图(方法+视图)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            AdminDTO dto = _AdminUsers.Get(id);
            var adminList = _role.GetList();
            ViewBag.adminList = adminList;
            ViewBag.CityList = _AdminUsers.GetCity();
            return View(dto);
        }
        /// <summary>
        /// 实现修改管理员(方法)
        /// </summary>
        /// <param name="adminDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditAdmin(AdminDTO adminDTO)
        { 
            var res=_AdminUsers.Update(adminDTO);   
            return Json(res);   
        }
        /// <summary>
        /// 删除角色(方法)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(long id)
        {
            var result = _AdminUsers.Delete(id);
            return Json(result);
        }
        /// <summary>
        /// 批量删除角色(方法)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var result = _AdminUsers.DeleteBatch(ids);
            return Json(result);
        }
    }
}